#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Set PhaseOrigin Manually					    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 02/20/2006                                             #
# Last Modification: 02/20/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 48
#
# MANUAL: Once all "Standard Scripts" processing is done, this script allows to manually correct the <B>phase origin</B> that is used for merging, and to center the final map. 
#
# MANUAL: You should open via <i>doubble-click</I> the <I>Non-symmetrized Map</I>, place the mouse over the feature that you want to have centered, and press <I>Shift+P</I>. Then leave the full-screen browser by hitting <I>Esc</I>. This then will have defined the <I>Phase Origin Change</I> value. 
#
# MANUAL: Now run this script, and it will add the <I>Phase Origin Change</I> to the <I>Phase Origin</I>, and re-calculate the final non-symmetrized projection map, using the new <I>Phase Origin</I> value. 
#
# RESET: show_hkl = "n"
# RESET: show_phares = "n"
# RESET: show_PSF = "n"
#
# DISPLAY: SYM
# DISPLAY: lattice
# DISPLAY: secondlattice
# DISPLAY: realcell
# DISPLAY: realang
# DISPLAY: TAXA
# DISPLAY: TANGL
# DISPLAY: comment
# DISPLAY: phaori
# DISPLAY: phaoriFouFilter
# DISPLAY: phaori_change
# DISPLAY: phaori_manual_target
# DISPLAY: npo_cntrs_step
# DISPLAY: npo_cntrs_below 
# DISPLAY: MergeIQMAX
# DISPLAY: mapdimpx
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set SYN_Unbending = ""
set ALAT = ""
set CS = ""
set KV = ""
set ML_doit = ""
set PHASEORI_done = ""
set RESMAX = ""
set RESMIN = ""
set SYM = ""
set SYN_Unbending = ""
set TANGL = ""
set TAXA = ""
set TLTANG = ""
set TLTAXA = ""
set TLTAXIS = ""
set beamtilt = ""
set ctfrev = ""
set imagename = ""
set imagenumber = ""
set imagesidelength = ""
set lattice = ""
set magnification = ""
set phacon = ""
set phaori = ""
set phaoriFouFilter = ""
set phaori_change = ""
set realang = ""
set realcell = ""
set revhk = ""
set revhnd = ""
set phaori_manual_target = ""
set revxsgn = ""
set rot180 = ""
set rot90 = ""
set secondlattice = ""
set sgnxch = ""
set stepdigitizer = ""
set tempfac = ""
set tempkeep = ""
set zstarrange = ""
set npo_cntrs_step = ""
set npo_cntrs_below = ""
set MergeIQMAX = ""
set movie_enable = ""
set RFACAMP = ""
set show_hkl = ""
set show_phares = ""
set show_PSF = ""
set mapdimpx = ""
#
#$end_vars
#
echo "<<@progress: 1>>"
#
set ccp4_setup = 'y'
set IS_2DX = yes
source ${proc_2dx}/initialize
#
set mapscale = "0.25"
set unicellnumber = "6"
#
echo lattice = ${lattice}
echo secondlattice = ${secondlattice}
#
set scriptname = 2dx_phaseorigin
#
\rm -f LOGS/${scriptname}.results
#
set aphfile = APH/image_ctfcor_fou_unbent_ctf.aph
echo "# IMAGE-IMPORTANT: ${aphfile} <APH: Input Unbending Amp&Phase File>" >> LOGS/${scriptname}.results
#
${proc_2dx}/linblock "Lattice is currently: ${lattice}"
#
set phaori_local = ${phaori}
#
set phaori_x = `echo ${phaori_local} | sed 's/,/ /g' | awk '{ s = $1 } END { print s }'`
set phaori_y = `echo ${phaori_local} | sed 's/,/ /g' | awk '{ s = $2 } END { print s }'`
set phaori_change_x = `echo ${phaori_change} | sed 's/,/ /g' | awk '{ s = $1 } END { print s }'`
set phaori_change_y = `echo ${phaori_change} | sed 's/,/ /g' | awk '{ s = $2 } END { print s }'`
#
set phaori_manual_targ_x = 0.0
set phaori_manual_targ_y = 0.0
if ( ${phaori_manual_target} == '1' ) then
  if ( ${phaori_change_x} != '0.0' && ${phaori_change_y} != '0.0' ) then
    set phaori_manual_targ_x = 180.0
    set phaori_manual_targ_y = 180.0
  endif
endif
#
set phaori_x = `echo ${phaori_x} ${phaori_change_x} ${phaori_manual_targ_x} | awk '{ s = $1 + $2 + $3 } END { print s }'`
set phaori_y = `echo ${phaori_y} ${phaori_change_y} ${phaori_manual_targ_y} | awk '{ s = $1 + $2 + $3 } END { print s }'`
#
set phaori_x = `echo ${phaori_x} | awk '{ if ( $1 >   180.0) { s = $1 - 360.0 } else {  s = $1 }} END { print s }'`
set phaori_x = `echo ${phaori_x} | awk '{ if ( $1 >   180.0) { s = $1 - 360.0 } else {  s = $1 }} END { print s }'`
set phaori_x = `echo ${phaori_x} | awk '{ if ( $1 <= -180.0) { s = $1 + 360.0 } else {  s = $1 }} END { print s }'`
set phaori_x = `echo ${phaori_x} | awk '{ if ( $1 <= -180.0) { s = $1 + 360.0 } else {  s = $1 }} END { print s }'`
set phaori_y = `echo ${phaori_y} | awk '{ if ( $1 >   180.0) { s = $1 - 360.0 } else {  s = $1 }} END { print s }'`
set phaori_y = `echo ${phaori_y} | awk '{ if ( $1 >   180.0) { s = $1 - 360.0 } else {  s = $1 }} END { print s }'`
set phaori_y = `echo ${phaori_y} | awk '{ if ( $1 <= -180.0) { s = $1 + 360.0 } else {  s = $1 }} END { print s }'`
set phaori_y = `echo ${phaori_y} | awk '{ if ( $1 <= -180.0) { s = $1 + 360.0 } else {  s = $1 }} END { print s }'`
#
set phaori_local = `echo ${phaori_x},${phaori_y}`
#
# FouFilter Unbending result is used:
echo "set phaoriFouFilter = ${phaori_local}" >> LOGS/${scriptname}.results
echo "set phaori = ${phaori_local}" >> LOGS/${scriptname}.results
#
if ( ${phaori_change} == "0.0,0.0" ) then
  echo ":: "
  echo ":: Phase origin not changed."
  echo ":: Phase origin is ${phaori}."
  echo " "
else
  echo ":: "
  echo ":: Phase origin change is  ${phaori_change}."
  echo ":: New phase origin is now ${phaori}."
  echo " "
  echo "set phaori_change = 0.0,0.0" >> LOGS/${scriptname}.results
  echo "set PHASEORI_done = y" >> LOGS/${scriptname}.results
endif
#
set prename = ""
set prefix = ""
#
echo "# IMAGE-IMPORTANT: ${prefix}${imagename}-p1.mrc <${prename}Non-symmetrized Map>"  >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${prefix}${imagename}-${SYM}.mrc <Old ${prename}${SYM}-symmetrized Map>" >> LOGS/${scriptname}.results
#
set ABANG = `echo $realang | awk '{s=180-$1} END {print s}'`
echo ABANG = ${ABANG}
set IAQP2 = 0
set IVERBOSE = 1
set LOGOUTPUT = F
set LPROTFOUFIL = T
set phastepnum = 1
set phastep = 0.1
#
#############################################################################
#
set SYM_sub = 'p1'
set spcgrp = "1"
set spcgroup_num = "1"
set CCP4_SYM = "1"
set ML_sym_val = "1"
#
set symmetry = 'p1'
set spcgrp_local = 1
set CCP4_SYM = 1
set rotate_to_Z = "no"
#
# origtilt does something strange when working with spcgrp>1. (TODO).
# Symmetrization is therefore here only done by the CCP4 programs.
#
set spcgrp_first = 1
echo spcgrp_first = ${spcgrp_first}
#
echo spcgrp = ${spcgrp}
echo SYM_sub = ${SYM_sub}
echo CCP4_SYM = ${CCP4_SYM}
#
#
#############################################################################
echo ":: "
${proc_2dx}/linblock "Sourcing generateMap script for Unbending reconstruction in p1 symmetry"
echo ":: "
#############################################################################
#############################################################################
#############################################################################
#
set prefix = ""
set prename = ""
#
source ${proc_2dx}/2dx_generateMap_sub.com
#
#############################################################################
#############################################################################
#############################################################################
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
  
